package com.thd.kc.utils;

import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.nio.file.Paths;

public class LuceneUtils {
    // 索引文件的位置
    public static Directory INDEX_DIRECTORY;

    // 中文分词器  false为最小分词  true为最大分词
    public static IKAnalyzer ANALYZER = new IKAnalyzer(false);




    public static void initGlobal(String indexFolderPath) throws Exception{
        INDEX_DIRECTORY = FSDirectory.open(Paths.get(indexFolderPath));
    }







    public static void initIndex(){
        System.out.println("init lucene index files ...");

        IndexWriter indexWriter = null;

        try{
            IKAnalyzer analyzer = new IKAnalyzer();
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            indexWriter = new IndexWriter(LuceneUtils.INDEX_DIRECTORY, config);

            // 创建一个Document对象
            Document doc = new Document();


            Field initField = new StoredField("init","");
            doc.add(initField);
            indexWriter.addDocument(doc);

        }catch (Exception e){
            e.printStackTrace();;
        }finally {
            try{
                indexWriter.close();
            }catch (Exception e){

            }
        }


    }





}
